<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a href="javascript:;" v-for="item in filterData.brands" :key="item.id"
           :class="{active: filterData.brands.selectedBrand === item.id}" @click="changeBrand(item.id)">{{
            item.name
          }}</a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a href="javascript:;" v-for="prop in item.properties" :key="prop.id"
           :class="{active: item.selectedProp === prop.id}" @click="changeProp(item,prop.id)">{{ prop.name }}</a>
      </div>
    </div>
  </div>
  <div v-else class=" sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px"/>
    <XtxSkeleton class="item" width="800px" height="40px"/>
    <XtxSkeleton class="item" width="600px" height="40px"/>
    <XtxSkeleton class="item" width="600px" height="40px"/>
    <XtxSkeleton class="item" width="600px" height="40px"/>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
import XtxSkeleton from '@/components/library/xtx-skeleton'

export default {
  name: 'SubFilter',
  components: { XtxSkeleton },
  setup (props, { emit }) {
    // 1. 获取数据
    // 2. 数据中需要全部选中，需要预览将来点击激活功能。默认选中全部
    // 3. 完成渲染
    const route = useRoute()
    //   监听二级类别id的变化获取赛选数据
    const filterData = ref([])
    const filterLoading = ref(false)
    watch(() => route.params.id, (newVal) => {
      // 变化后的id有值 且处在二级类目路由下
      if (newVal && `/category/sub/${newVal}` === route.path) {
        filterLoading.value = true
        // 请求获取数据
        findSubCategoryFilter(route.params.id).then(data => {
          // 每一组可选的筛选条件缺失“全部”这个条件，处理数据，加上“全部”
          // 给每一组数据加上一个选中的ID
          // 1.品牌
          data.result.brands.selectedBrand = null
          data.result.brands.unshift({
            id: null,
            name: '全部'
          })
          // 2.属性
          data.result.saleProperties.forEach(item => {
            item.selectedProp = null
            item.properties.unshift({
              id: null,
              name: '全部'
            })
          })
          // 设置修改的数据
          filterData.value = data.result
          filterLoading.value = false
        })
      }
    }, { immediate: true })

    // 获取赛选参数的函数
    const getFilterParams = () => {
      const obj = {
        brandId: null,
        attrs: []
      }
      //  参考数据： {brandId: '', attrs:[groupName:'', propertyName:'']}
      // 品牌
      obj.brandId = filterData.value.brands.selectedBrand
      // 销售属性
      filterData.value.saleProperties.forEach(item => {
        if (item.selectedProp) {
          const prop = item.properties.find(prop => prop.id === item.selectedProp)
          obj.attrs.push({
            groupName: item.name,
            propertyName: prop.name
          })
        }
      })
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }

    // 1.记录当前选中的品牌
    const changeBrand = (brandId) => {
      if (brandId === filterData.value.brands.selectedBrand) return
      filterData.value.brands.selectedBrand = brandId
      emit('filter-change', getFilterParams())
    }
    // 2.记录当前选中的销售属性
    const changeProp = (item, propId) => {
      if (propId === item.selectedProp) return
      item.selectedProp = propId
      emit('filter-change', getFilterParams())
    }

    return {
      filterData,
      filterLoading,
      changeBrand,
      changeProp
    }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;

  .item {
    display: flex;
    line-height: 40px;

    .head {
      width: 80px;
      color: #999;
    }

    .body {
      flex: 1;

      a {
        margin-right: 36px;
        transition: all .3s;
        display: inline-block;

        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }

  .xtx-skeleton {
    padding: 10px 0;
  }
}
</style>
